package com.data.util

import java.io.UnsupportedEncodingException
import java.security.MessageDigest


object Md5CodeUtils {

    /**
     * md5生成32位密文
     *
     * @param inStr
     * @return
     */

    fun md5(inStr: String): String {
        val md5: MessageDigest
        try {
            md5 = MessageDigest.getInstance("MD5")
        } catch (e: Exception) {
            println(e.toString())
            e.printStackTrace()
            return ""
        }

        val byteArray: ByteArray

        try {
            byteArray = inStr.toByteArray(charset("UTF-8"))
        } catch (e: UnsupportedEncodingException) {
            e.printStackTrace()
            return ""
        }

        val md5Bytes = md5.digest(byteArray)
        val hexValue = StringBuffer()
        for (i in md5Bytes.indices) {
            val `val` = md5Bytes[i].toInt() and 0xff
            if (`val` < 16) {
                hexValue.append("0")
            }
            hexValue.append(Integer.toHexString(`val`))
        }
        return hexValue.toString()
    }


    @JvmStatic
    fun main(args: Array<String>) {
        val s = Md5CodeUtils.md5("abcd7015847230")
        println(s)
    }

}
